package com.fan.teamer.controller;

import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;

import com.fan.teamer.global.TeamerGlobalVariables;
import com.fan.teamer.model.domain.Event;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Team;
import com.fan.teamer.process.AddMemberToTeamProcess;
import com.fan.teamer.process.EventCreateProcess;
import com.fan.teamer.process.EventDeleteProcess;
import com.fan.teamer.process.EventUpdateProcess;
import com.fan.teamer.process.MemberCreateProcess;
import com.fan.teamer.process.MemberDeleteProcess;
import com.fan.teamer.process.MemberJoinEventProcess;
import com.fan.teamer.process.MemberQuitEventProcess;
import com.fan.teamer.process.RemoveMemberFromTeamProcess;
import com.fan.teamer.process.TeamCreateProcess;
import com.fan.teamer.process.TeamDeleteProcess;
import com.fan.teamer.process.TeamMemberUpdateProcess;
import com.fan.teamer.service.AttendanceService;
import com.fan.teamer.service.EventService;
import com.fan.teamer.service.MemberService;
import com.fan.teamer.service.MessageService;
import com.fan.teamer.service.RoleService;
import com.fan.teamer.service.TeamMemberService;
import com.fan.teamer.service.TeamService;

@ManagedBean(name = "testBean")
@RequestScoped
public class TestBean extends TeamerBean {

	private static Logger logger = Logger.getLogger(TestBean.class);

	@ManagedProperty(value = "#{memberService}")
	private MemberService memberService;

	@ManagedProperty(value = "#{roleService}")
	private RoleService roleService;

	@ManagedProperty(value = "#{teamService}")
	private TeamService teamService;

	@ManagedProperty(value = "#{teamMemberService}")
	private TeamMemberService teamMemberService;

	@ManagedProperty(value = "#{attendanceService}")
	private AttendanceService attendanceService;

	@ManagedProperty(value = "#{eventService}")
	private EventService eventService;

	@ManagedProperty(value = "#{messageService}")
	private MessageService messageService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		TestBean.logger = logger;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public TeamMemberService getTeamMemberService() {
		return teamMemberService;
	}

	public void setTeamMemberService(TeamMemberService teamMemberService) {
		this.teamMemberService = teamMemberService;
	}

	public AttendanceService getAttendanceService() {
		return attendanceService;
	}

	public void setAttendanceService(AttendanceService attendanceService) {
		this.attendanceService = attendanceService;
	}

	public EventService getEventService() {
		return eventService;
	}

	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}

	public MessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public String memberMockTestAction() {
		logger.info("memberMockTestAction...");

		// add ten members
		for (int i = 0; i < 10; i++) {
			MemberCreateProcess memberCreateProcess = new MemberCreateProcess();
			memberCreateProcess.setMemberService(memberService);
			memberCreateProcess.setRoleService(roleService);
			memberCreateProcess.setEmail("test" + i + "@teamer.com");
			memberCreateProcess.setFirstname("Test");
			memberCreateProcess.setLastname("Test");
			memberCreateProcess.setPassword("test");
			memberCreateProcess.setConfirmPassword("test");
			memberCreateProcess.setGender("Male");
			memberCreateProcess.setPhoto("");
			memberCreateProcess.setDescription("hello test");
			memberCreateProcess.setCountry("Australia");
			memberCreateProcess.setState("NSW");
			memberCreateProcess.setCity("Sydney");
			memberCreateProcess.setAge(20);
			memberCreateProcess.setContactVisible(1);
			memberCreateProcess.setEnabled(1);

			int result = memberCreateProcess.process();

			logger.info(result);
		}

		return "test.faces";
	}

	public String checkMemberMockAction() {
		super.setMessage("Member size: " + memberService.getAllMembers().size());
		return "test.faces";
	}

	public String memberMockDeleteAction() {
		logger.info("memberMockTestAction...");

		// add ten members
		for (int i = 0; i < 10; i++) {

			Member member = memberService.getMember("test" + i + "@teamer.com");
			MemberDeleteProcess memberDeleteProcess = new MemberDeleteProcess();
			memberDeleteProcess.setMemberService(memberService);
			memberDeleteProcess.setRoleService(roleService);
			memberDeleteProcess.setMember(member);
			int result = memberDeleteProcess.process();

			logger.info(result);
		}

		return "test.faces";
	}

	public String teamMockAddTest() {
		Member operator = memberService.getMember("guest@teamer.com");

		for (int i = 0; i < 10; i++) {
			TeamCreateProcess teamCreateProcess = new TeamCreateProcess();
			teamCreateProcess.setTeamService(teamService);
			teamCreateProcess.setTeamMemberService(teamMemberService);
			teamCreateProcess.setMemberService(memberService);

			teamCreateProcess.setAgeProfile("30");
			teamCreateProcess.setArea("Meadowbank");
			teamCreateProcess.setAttribute("");
			teamCreateProcess.setCity("Sydney");
			teamCreateProcess.setCountry("Australia");
			teamCreateProcess.setDescription("Test team No: " + i);
			teamCreateProcess.setGender("male");
			teamCreateProcess.setIcon("");
			teamCreateProcess.setOperator(operator);
			teamCreateProcess.setName("Test Team " + i);
			teamCreateProcess.setSport("Soccer");
			teamCreateProcess.setState("NSW");
			teamCreateProcess.setTimezone("1");
			// teamCreateProcess.setInitiator(member.getLastname());

			int result = teamCreateProcess.process();
			logger.info(result);
		}

		return "test.faces";
	}

	public String teamMockCheckTest() {
		super.setMessage("Team size: " + teamService.getSize());
		return "test.faces";
	}

	public String teamMockDeleteTest() {

		Member member = memberService.getMember("guest@teamer.com");
		for (Team team : teamService.getAllTeams()) {

			TeamDeleteProcess teamDeleteProcess = new TeamDeleteProcess();
			teamDeleteProcess.setTeamService(teamService);
			teamDeleteProcess.setTeamMemberService(teamMemberService);
			teamDeleteProcess.setMemberService(memberService);
			teamDeleteProcess.setOperator(member);
			teamDeleteProcess.setTeam(team);

			int result = teamDeleteProcess.process();
			logger.info(result);
		}
		return "test.faces";
	}

	public String addMemberToTeamMockAction() {
		List<Member> members = memberService.getAllMembers();

		Team team = teamService.getAllTeams().get(0);

		for (Member member : members) {

			AddMemberToTeamProcess addMemberToTeamProcess = new AddMemberToTeamProcess();
			addMemberToTeamProcess.setMemberService(memberService);
			addMemberToTeamProcess.setTeamService(teamService);
			addMemberToTeamProcess.setTeamMemberService(teamMemberService);

			addMemberToTeamProcess.setMember(member);
			addMemberToTeamProcess.setTeam(team);
			addMemberToTeamProcess
					.setMemberRole(TeamerGlobalVariables.PARTICIPANT);

			int result = addMemberToTeamProcess.process();

			logger.info(result);

		}
		return "test.faces";
	}

	public String checkMemberInTeamMockAction() {

		Team team = teamService.getAllTeams().get(0);

		setMessage("Team " + team.getName() + " in Country: "
				+ team.getCountry() + " has "
				+ teamService.getTeamMembers(team).size() + " !");

		return "test.faces";
	}

	public String deleteMemberFromTeamMockAction() {
		Team team = teamService.getAllTeams().get(0);

		Member operator = memberService.getMember("guest@teamer.com");

		for (Member memberToBeRemoved : teamService.getTeamMembers(team)) {
			RemoveMemberFromTeamProcess removeMemberFromTeamProcess = new RemoveMemberFromTeamProcess();
			removeMemberFromTeamProcess.setMemberService(memberService);
			removeMemberFromTeamProcess.setTeamService(teamService);
			removeMemberFromTeamProcess.setTeamMemberService(teamMemberService);

			removeMemberFromTeamProcess.setOperator(operator);
			removeMemberFromTeamProcess.setMember(memberToBeRemoved);
			removeMemberFromTeamProcess.setTeam(team);

			int result = removeMemberFromTeamProcess.process();

			logger.info(result);

		}

		return "test.faces";
	}

	public String changeRoleCoOrganiserMembersInTeamMockAction() {
		Team team = teamService.getAllTeams().get(0);

		Member operator = memberService.getMember("guest@teamer.com");

		for (Member member : teamService.getTeamMembers(team)) {
			TeamMemberUpdateProcess teamMemberUpdateProcess = new TeamMemberUpdateProcess();
			teamMemberUpdateProcess.setMemberService(memberService);
			teamMemberUpdateProcess.setTeamService(teamService);
			teamMemberUpdateProcess.setTeamMemberService(teamMemberService);
			teamMemberUpdateProcess.setOperator(operator);
			teamMemberUpdateProcess.setMember(member);
			teamMemberUpdateProcess.setTeam(team);
			teamMemberUpdateProcess.setRole(TeamerGlobalVariables.CO_ORGANIZER);

			int result = teamMemberUpdateProcess.process();

			logger.info(result);

		}

		return "test.faces";

	}

	public String changeRoleParticipantMembersInTeamMockAction() {
		Team team = teamService.getAllTeams().get(0);

		Member operator = memberService.getMember("guest@teamer.com");

		for (Member member : teamService.getTeamMembers(team)) {
			TeamMemberUpdateProcess teamMemberUpdateProcess = new TeamMemberUpdateProcess();
			teamMemberUpdateProcess.setMemberService(memberService);
			teamMemberUpdateProcess.setTeamService(teamService);
			teamMemberUpdateProcess.setTeamMemberService(teamMemberService);
			teamMemberUpdateProcess.setOperator(operator);
			teamMemberUpdateProcess.setMember(member);
			teamMemberUpdateProcess.setTeam(team);
			teamMemberUpdateProcess.setRole(TeamerGlobalVariables.PARTICIPANT);

			int result = teamMemberUpdateProcess.process();

			logger.info(result);

		}
		return "test.faces";
	}

	public String eventCreateMockAction() {
		Member operator = memberService.getMember("guest@teamer.com");
		int index = 0;
		for (Team team : teamService.getAllTeams()) {
			EventCreateProcess eventCreateProcess = new EventCreateProcess();
			eventCreateProcess.setMemberService(memberService);
			eventCreateProcess.setTeamService(teamService);
			eventCreateProcess.setEventService(eventService);

			eventCreateProcess.setName("Event_" + index);
			eventCreateProcess.setDescription("Event_" + index + " in team: "
					+ team.getName() + "  " + team.getCountry());
			eventCreateProcess.setTeam(team);
			eventCreateProcess
					.setEventTime(new Date(System.currentTimeMillis()));
			eventCreateProcess.setIsPublic(TeamerGlobalVariables.TRUE);
			eventCreateProcess.setIsRecurring(TeamerGlobalVariables.TRUE);
			eventCreateProcess.setLocation("Meadowbank");
			eventCreateProcess.setOperator(operator);
			eventCreateProcess.setType("Soccer");

			int result = eventCreateProcess.process();

			logger.info(result);
			index++;
		}
		setMessage(teamService.getSize() + " events created and we have "
				+ eventService.getSize() + " events now!");

		return "test.faces";
	}

	public String eventUpdateMockTest() {
		Member operator = memberService.getMember("guest@teamer.com");
		for (Event event : eventService.getAllEvents()) {
			Team team = teamService.getTeam(event.getTeamId());
			EventUpdateProcess eventUpdateProcess = new EventUpdateProcess();
			eventUpdateProcess.setTeamService(teamService);
			eventUpdateProcess.setMemberService(memberService);
			eventUpdateProcess.setEventService(eventService);
			eventUpdateProcess.setDescription("new description "
					+ event.getDescription());
			eventUpdateProcess.setEvent(event);
			eventUpdateProcess
					.setEventTime(new Date(System.currentTimeMillis()));
			eventUpdateProcess.setIsPublic(TeamerGlobalVariables.FALSE);
			eventUpdateProcess.setIsRecurring(TeamerGlobalVariables.FALSE);
			eventUpdateProcess.setLocation("Shanghai");
			eventUpdateProcess.setOperator(operator);
			eventUpdateProcess.setName("New name: " + event.getEventName());
			eventUpdateProcess.setType("Basketball");
			eventUpdateProcess.setTeam(team);

			int result = eventUpdateProcess.process();

			logger.info(result);
		}

		setMessage(eventService.getSize() + " Events updated and we have "
				+ eventService.getSize() + " events now!");
		return "test.faces";
	}

	public String eventDeleteMockTest() {
		Member operator = memberService.getMember("guest@teamer.com");
		int size = eventService.getSize();
		for (Event event : eventService.getAllEvents()) {
			Team team = teamService.getTeam(event.getTeamId());
			EventDeleteProcess eventDeleteProcess = new EventDeleteProcess();
			eventDeleteProcess.setAttendanceService(attendanceService);
			eventDeleteProcess.setEvent(event);
			eventDeleteProcess.setEventService(eventService);
			eventDeleteProcess.setMemberService(memberService);
			eventDeleteProcess.setOperator(operator);
			eventDeleteProcess.setTeam(team);
			eventDeleteProcess.setTeamService(teamService);

			int result = eventDeleteProcess.process();

			logger.info(result);
		}
		setMessage(size + " Events deleted and we have "
				+ eventService.getSize() + " events now!");
		return "test.faces";
	}

	public String attendEventMockTest() {
		for (Team team : teamService.getAllTeams()) {
			for (Event event : teamService.getTeamEvents(team)) {
				for (Member member : teamService.getTeamMembers(team)) {
					MemberJoinEventProcess memberJoinEventProcess = new MemberJoinEventProcess();
					memberJoinEventProcess
							.setAttendanceService(attendanceService);
					memberJoinEventProcess.setEvent(event);
					memberJoinEventProcess.setEventService(eventService);
					memberJoinEventProcess.setMember(member);
					memberJoinEventProcess.setMemberService(memberService);
					memberJoinEventProcess.setTeamService(teamService);

					int result = memberJoinEventProcess.process();

					logger.info(result);
				}
			}
		}
		return "test.faces";
	}

	public String quitEventMockTest() {
		for (Team team : teamService.getAllTeams()) {
			for (Event event : teamService.getTeamEvents(team)) {
				for (Member member : teamService.getTeamMembers(team)) {
					MemberQuitEventProcess memberQuitEventProcess = new MemberQuitEventProcess();
					memberQuitEventProcess
							.setAttendanceService(attendanceService);
					memberQuitEventProcess.setEvent(event);
					memberQuitEventProcess.setEventService(eventService);
					memberQuitEventProcess.setMember(member);
					memberQuitEventProcess.setMemberService(memberService);
					memberQuitEventProcess.setTeamService(teamService);

					int result = memberQuitEventProcess.process();

					logger.info(result);
				}
			}
		}

		return "test.faces";
	}

}
